package com.ruoyi.violate.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.violate.domain.ViolateWords;
import com.ruoyi.violate.service.IViolateWordsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 违规词汇Controller
 * 
 * @author 谭拓宇
 * @date 2022-12-03
 */
@RestController
@RequestMapping("/violate/words")
public class ViolateWordsController extends BaseController
{
    @Autowired
    private IViolateWordsService violateWordsService;

    /**
     * 查询违规词汇列表
     */
    @PreAuthorize("@ss.hasPermi('violate:words:list')")
    @GetMapping("/list")
    public TableDataInfo list(ViolateWords violateWords)
    {
        startPage();
        List<ViolateWords> list = violateWordsService.selectViolateWordsList(violateWords);
        return getDataTable(list);
    }

    /**
     * 导出违规词汇列表
     */
    @PreAuthorize("@ss.hasPermi('violate:words:export')")
    @Log(title = "违规词汇", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ViolateWords violateWords)
    {
        List<ViolateWords> list = violateWordsService.selectViolateWordsList(violateWords);
        ExcelUtil<ViolateWords> util = new ExcelUtil<ViolateWords>(ViolateWords.class);
        util.exportExcel(response, list, "违规词汇数据");
    }

    /**
     * 获取违规词汇详细信息
     */
    @PreAuthorize("@ss.hasPermi('violate:words:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(violateWordsService.selectViolateWordsById(id));
    }

    /**
     * 新增违规词汇
     */
    @PreAuthorize("@ss.hasPermi('violate:words:add')")
    @Log(title = "违规词汇", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ViolateWords violateWords)
    {
        return toAjax(violateWordsService.insertViolateWords(violateWords));
    }

    /**
     * 违规词汇导入
     */
    @PreAuthorize("@ss.hasPermi('violate:words:add')")
    @PostMapping("import")
    public AjaxResult importFile(@RequestBody ViolateWords violateWords) {
        if(StringUtils.isEmpty(violateWords.getFilePath()))
            return AjaxResult.error("请上传文件");
        return toAjax(violateWordsService.importFile(violateWords.getFilePath()));
    }

    /**
     * 修改违规词汇
     */
    @PreAuthorize("@ss.hasPermi('violate:words:edit')")
    @Log(title = "违规词汇", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ViolateWords violateWords)
    {
        return toAjax(violateWordsService.updateViolateWords(violateWords));
    }

    /**
     * 删除违规词汇
     */
    @PreAuthorize("@ss.hasPermi('violate:words:remove')")
    @Log(title = "违规词汇", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(violateWordsService.deleteViolateWordsByIds(ids));
    }

    /**
     * 一键去重
     */
//    @PreAuthorize("@ss.hasPermi('violate:words:distinct')")
    @GetMapping("/distinct")
    public AjaxResult distinct(){
        int i = violateWordsService.distinctViolateWords();
        return  toAjax(i+1) ;
    }
}
